package com.project.im.web.service;

import com.project.im.common.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TokenService {

    private static final String SECRET = "uX2n0DfE4RmI9vLq8tGs1JbNwHkZpVc/xrD5KgTYaUWQsFpoLMzQyvbJHkACeE3A==";

    private static final long EXPIRATION_MS = 86400 * 1000 * 7;

    public String generateToken(String subject) {
        return JwtUtils.generateToken(SECRET, EXPIRATION_MS, subject);
    }

    public Claims parseToken(String token) {
        try {
            return JwtUtils.parseToken(SECRET, token);
        } catch (Exception e) {
            return null;
        }
    }

    public boolean isTokenExpired(Claims claims) {
        return JwtUtils.extractExpiration(claims).before(new Date());
    }
}
